var express = require('express');
var router = express.Router();
var {User} = require('../model/model')
let {
    academyModel,
    stageModel,
    questionModel,
    paperModel,
    adminModel}
    =require("../model/model")

//试题表的渲染搜索分页
router.get('/question_list',async(req,res)=>{
    let {cate,difficulty,page,pagesize,id}=req.query
    let arr=[]
    if(id){
        arr.push({belong:id})
    }
    if(cate!=0){
        arr.push({cate})
    }
    if(difficulty!=0){
        arr.push({difficulty})
    }
    if(arr.length==0){
        arr.push({})
    }
    let total=await questionModel.find({$and:arr}).countDocuments()
    
    let question_list=await questionModel.find({$and:arr}).populate('belong').skip((page-1)*pagesize).limit(pagesize)
    res.send({
        question_list,
        total
    })
})

//添加试题 
router.post('/question_add',async(req,res)=>{
    let {obj}=req.body
    let op= []
    obj.options.forEach((ele)=>{
        if(ele.title!=""){
            op.push(ele)
        }
    })
    obj.options=op
    await questionModel.create(obj)
    res.send({
        msg:'添加成功'
    })
})
//修改试题
router.post('/question_edit',async(req,res)=>{
    let {obj,id}=req.body
    await questionModel.updateOne({_id:id},obj)
    res.send({
        msg:'修改成功'
    })
})
//批量删除
router.post('/question_del',async(req,res)=>{
    let {ids}=req.body
    await questionModel.deleteMany({_id:{$in:ids}})
    res.send({
        msg:'删除成功'
    })
})

//修改状态
router.post('/question_status',async(req,res)=>{
    let{id,status}=req.body
    if(status==1){
        await questionModel.updateOne({_id:id},{status:0})
    }else{
        await questionModel.updateOne({_id:id},{status:1})
    }
    res.send({
        code:200
    })
    
})

//试题表示的渲染
router.get('/question_list2',async(req,res)=>{
    let question_list=await questionModel.find()
    res.send({
        question_list
    })
})

//阶段表的渲染
router.get('/stage_list',async(req,res)=>{
    let {value,page,pagesize}=req.query
    let name=new RegExp(value)
    let arr=[]
    if(name){
        arr.push({name:name})
    }
    //分页
    let skip=(page-1)*pagesize
    let limit=pagesize
    let total=await stageModel.find({$and:arr}).countDocuments()
    let stage_list=await stageModel.find({$and:arr}).populate('creator').skip(skip).limit(limit)

    res.send({
        stage_list,
        total
    })
})
//阶段添加
router.post('/stage_add',async(req,res)=>{
    let{name,academy,creator}=req.body 
    await stageModel.create({name,academy,creator})
    res.send({
        msg:'添加成功'
    })
})
//阶段删除
router.post('/stage_del',async(req,res)=>{
    let{id}=req.body
    await stageModel.deleteOne({_id:id})
    res.send({
        msg:'删除成功'
    })
})
//阶段回显
router.get('/stage_echo',async(req,res)=>{
    let{id}=req.query
    let stage=await stageModel.findOne({_id:id})
    res.send({
        stage
    })
})
//阶段修改
router.post('/stage_edit',async(req,res)=>{
    let{id,name,academy,creator}=req.body
    await stageModel.updateOne({_id:id},{name,academy,creator})
    res.send({
        msg:'修改成功'
    })
})

//学院表的渲染
router.get('/academy_list',async(req,res)=>{
    let academy_list=await academyModel.find()
    res.send({
        academy_list
    })
})


module.exports = router;